//
//  ZSNAVPlayerViewController.swift
//  AgoraAVPlayerZhaosn
//
//  Created by zhaosuning on 2022/1/5.
//

import UIKit
import AgoraRtcKit

class ZSNAVPlayerViewController: UIViewController, AgoraRtcEngineDelegate {

    var videoView: ZSNAVPlayerView!
    var agoraKit: AgoraRtcEngineKit!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "AVPlayer"
        self.view.backgroundColor = UIColor.white
        
        videoView = ZSNAVPlayerView()
        videoView.backgroundColor = UIColor.yellow
        videoView.frame = CGRect.init(x: 0, y: 100, width: 300, height: 300)
        self.view.addSubview(videoView)
        
        
        
        let btnInitRtc = UIButton.init(frame: CGRect(x: 30, y: 90, width: 160, height: 50))
        btnInitRtc.backgroundColor = UIColor.green
        btnInitRtc.setTitle("初始化", for: .normal)
        btnInitRtc.layer.cornerRadius = 23.5
        btnInitRtc.titleLabel?.textAlignment = .center
        btnInitRtc.layer.masksToBounds = true
        btnInitRtc.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnInitRtc.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnInitRtc.addTarget(self, action: #selector(btnInitRtcClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnInitRtc)
        
        let btnJoinChannel = UIButton.init(frame: CGRect(x: 200, y: 90, width: 160, height: 50))
        btnJoinChannel.backgroundColor = UIColor.green
        btnJoinChannel.setTitle("点击进入频道", for: .normal)
        //btnGot.setBackgroundImage(UIImage.init(named: "ic_take_photo_btn_bg"), for: UIControl.State.normal)
        btnJoinChannel.layer.cornerRadius = 23.5
        btnJoinChannel.titleLabel?.textAlignment = .center
        btnJoinChannel.layer.masksToBounds = true
        //btnGot.titleLabel?.font = UIFont.init(name: "Montserrat-Medium", size: 18 * SCREENWB)
        btnJoinChannel.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnJoinChannel.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnJoinChannel.addTarget(self, action: #selector(btnJoinChannelClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnJoinChannel)
        
        
        let btnRoleBroadcaster = UIButton.init(frame: CGRect(x: 30, y: 150, width: 160, height: 50))
        btnRoleBroadcaster.backgroundColor = UIColor.green
        btnRoleBroadcaster.setTitle("变为主播", for: .normal)
        btnRoleBroadcaster.layer.cornerRadius = 23.5
        btnRoleBroadcaster.titleLabel?.textAlignment = .center
        btnRoleBroadcaster.layer.masksToBounds = true
        btnRoleBroadcaster.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnRoleBroadcaster.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnRoleBroadcaster.addTarget(self, action: #selector(btnRoleBroadcasterClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnRoleBroadcaster)
        
        let btnRoleAudience = UIButton.init(frame: CGRect(x: 30, y: 150, width: 160, height: 50))
        btnRoleAudience.backgroundColor = UIColor.green
        btnRoleAudience.setTitle("变为观众", for: .normal)
        btnRoleAudience.layer.cornerRadius = 23.5
        btnRoleAudience.titleLabel?.textAlignment = .center
        btnRoleAudience.layer.masksToBounds = true
        btnRoleAudience.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnRoleAudience.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnRoleAudience.addTarget(self, action: #selector(btnRoleAudienceClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnRoleAudience)
        
        let btngameStreaming = UIButton.init(frame: CGRect(x: 30, y: 210, width: 160, height: 50))
        btngameStreaming.backgroundColor = UIColor.green
        btngameStreaming.setTitle("gameStreaming", for: .normal)
        btngameStreaming.layer.cornerRadius = 23.5
        btngameStreaming.titleLabel?.textAlignment = .center
        btngameStreaming.layer.masksToBounds = true
        btngameStreaming.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btngameStreaming.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btngameStreaming.addTarget(self, action: #selector(btngameStreamingClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btngameStreaming)
        
        let btnChatRoom = UIButton.init(frame: CGRect(x: 30, y: 270, width: 160, height: 50))
        btnChatRoom.backgroundColor = UIColor.green
        btnChatRoom.setTitle("ChatRoom", for: .normal)
        btnChatRoom.layer.cornerRadius = 23.5
        btnChatRoom.titleLabel?.textAlignment = .center
        btnChatRoom.layer.masksToBounds = true
        btnChatRoom.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnChatRoom.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnChatRoom.addTarget(self, action: #selector(btnChatRoomClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnChatRoom)
        
        let btnPlay = UIButton.init(frame: CGRect(x: 210, y: 270, width: 160, height: 50))
        btnPlay.backgroundColor = UIColor.green
        btnPlay.setTitle("播放", for: .normal)
        btnPlay.layer.cornerRadius = 23.5
        btnPlay.titleLabel?.textAlignment = .center
        btnPlay.layer.masksToBounds = true
        btnPlay.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnPlay.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnPlay.addTarget(self, action: #selector(btnPlayClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnPlay)
    }
    
    @objc func btnInitRtcClick() {
        joinChannella()
    }
    
    @objc func btnJoinChannelClick() {
        let channelName = "zsncna"
        let option = AgoraRtcChannelMediaOptions()
        let result = agoraKit.joinChannel(byToken: ZSNComDef.Token, channelId: channelName, info: nil, uid: 0, options: option)
        if result != 0 {
            NSLog("打印了 进入频道失败 \(result)")
        }
        else {
            NSLog("打印了 已进入频道")
        }
    }
    
    @objc func btnRoleBroadcasterClick() {
        agoraKit.setClientRole(.broadcaster)
    }
    
    @objc func btnRoleAudienceClick() {
        agoraKit.setClientRole(.audience)
    }
    
    @objc func btngameStreamingClick() {
        agoraKit.setAudioProfile(.musicHighQuality, scenario: .gameStreaming)
    }
    
    @objc func btnChatRoomClick() {
        agoraKit.setAudioProfile(.default, scenario: .chatRoomEntertainment)
    }
    
    @objc func btnPlayClick() {
        videoView.playVideo(itemIndex: 0)
    }
    
    func joinChannella() {
        
        let config = AgoraRtcEngineConfig()
        config.appId = ZSNComDef.AppId
        config.areaCode = 0xFFFFFFFF
        let logConfig = AgoraLogConfig()
        logConfig.level = .info
        config.logConfig = logConfig
        
        agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
        
        
        let resolution = CGSize(width: 1280, height: 720)
        let fps = AgoraVideoFrameRate.fps15 //.fps60
        let orientation = AgoraVideoOutputOrientationMode.fixedLandscape
        
        // become a live broadcaster
        
        agoraKit.setAudioProfile(.musicHighQualityStereo, scenario: .gameStreaming)
        //agoraKit.setAudioProfile(.default, scenario: .chatRoomEntertainment)
        agoraKit.setChannelProfile(.liveBroadcasting)
        //agoraKit.setClientRole(.audience)
        
        agoraKit.setClientRole(.broadcaster)
        
        // enable video module and set up video encoding configs
        agoraKit.enableAudio()
        agoraKit.enableVideo()
        agoraKit.setVideoEncoderConfiguration(AgoraVideoEncoderConfiguration(size: resolution,
                frameRate: fps,
                bitrate: AgoraVideoBitrateStandard,
                orientationMode: orientation))
        
        
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurWarning warningCode: AgoraWarningCode) {
        
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) {
        
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) {
        NSLog("打印了 didJoinChannel")
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
        
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid: UInt, reason: AgoraUserOfflineReason) {
        
    }
    
    func rtcEngineConnectionDidLost(_ engine: AgoraRtcEngineKit) {
        
    }

}
